# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=firefox-developer-edition
pkgver=126.0_beta9
_pkgver=${pkgver/_beta/b}
_releasedate=2024-05-04
pkgrel=0
pkgdesc="Firefox web browser"
url="https://www.firefox.com/"
# s390x and riscv64 blocked by rust and cargo
# armhf: https://bugzilla.mozilla.org/show_bug.cgi?id=1680495
# ppc64le: ld.lld: error: undefined hidden symbol: something about video
# x86/armv7: who is using this there?
arch="x86_64 aarch64"
license="GPL-3.0-only AND LGPL-2.1-only AND LGPL-3.0-only AND MPL-2.0"
depends="
	ffmpeg-libavcodec
	"
makedepends="
	alsa-lib-dev
	automake
	bsd-compat-headers
	cargo
	cbindgen
	clang
	clang-libclang
	dbus-glib-dev
	gettext
	gtk+3.0-dev
	hunspell-dev
	icu-dev
	libevent-dev
	libffi-dev
	libjpeg-turbo-dev
	libnotify-dev
	libogg-dev
	libtheora-dev
	libtool
	libvorbis-dev
	libvpx-dev
	libwebp-dev
	libxcomposite-dev
	libxt-dev
	lld
	llvm-dev
	m4
	mesa-dev
	nasm
	nodejs
	nspr-dev
	nss-dev
	pipewire-dev
	pulseaudio-dev
	python3
	sed
	wasi-sdk
	wireless-tools-dev
	zip
	"
source="https://ftp.mozilla.org/pub/firefox/releases/$_pkgver/source/firefox-$_pkgver.source.tar.xz
	fix-fortify-system-wrappers.patch
	fix-rust-target.patch
	fix-webrtc-glibcisms.patch
	install-dir.patch
	lfs64.patch
	no-ccache-stats.patch
	python-deps.patch
	rust-lto-thin.patch
	sandbox-fork.patch
	sandbox-largefile.patch
	sandbox-sched_setscheduler.patch
	icu74.patch

	stab.h

	firefox-developer-edition.desktop
	distribution.ini
	vendor-prefs.js
	"
options="!check" # no tests
builddir="$srcdir/firefox-${pkgver/_*/}"

_mozappdir=/usr/lib/firefox-developer-edition

# help our shared-object scanner to find the libs
ldpath="$_mozappdir"
sonameprefix="$pkgname:"

# we need this because cargo verifies checksums of all files in vendor
# crates when it builds and gives us no way to override or update the
# file sanely... so just clear out the file list
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' third_party/rust/$1/.cargo-checksum.json
}

export CFLAGS="${CFLAGS/-fstack-clash-protection/} -g0 -O2"
export CXXFLAGS="${CXXFLAGS/-fstack-clash-protection/} -g0 -O2 -Wno-deprecated-builtins -Wno-deprecated-declarations"

prepare() {
	default_prepare

	cp "$srcdir"/stab.h toolkit/crashreporter/google-breakpad/src/

	_clear_vendor_checksums audio_thread_priority

	cat > base-mozconfig <<-EOF
	# disable unwanted things
	ac_add_options --disable-bootstrap
	ac_add_options --disable-cargo-incremental
	ac_add_options --disable-crashreporter
	ac_add_options --disable-elf-hack
	ac_add_options --disable-install-strip
	ac_add_options --disable-jemalloc
	ac_add_options --disable-strip
	ac_add_options --disable-tests
	ac_add_options --disable-updater

	# features
	ac_add_options --enable-alsa
	ac_add_options --enable-dbus
	ac_add_options --enable-default-toolkit=cairo-gtk3-wayland
	ac_add_options --enable-ffmpeg
	ac_add_options --enable-hardening
	ac_add_options --enable-linker=lld
	ac_add_options --enable-lto=cross
	ac_add_options --enable-necko-wifi
	ac_add_options --enable-official-branding
	ac_add_options --enable-optimize="$CFLAGS"
	ac_add_options --enable-pulseaudio
	ac_add_options --enable-release
	ac_add_options --enable-sandbox
	ac_add_options --enable-update-channel=aurora

	# system libs
	ac_add_options --enable-system-pixman
	ac_add_options --with-system-ffi
	ac_add_options --with-system-icu
	ac_add_options --with-system-jpeg
	ac_add_options --with-system-libevent
	ac_add_options --with-system-libvpx
	ac_add_options --with-system-nspr
	ac_add_options --with-system-nss
	ac_add_options --with-system-png
	ac_add_options --with-system-webp
	ac_add_options --with-system-zlib

	# misc
	ac_add_options --prefix=/usr
	ac_add_options --allow-addon-sideload
	ac_add_options --with-distribution-id=org.alpinelinux
	ac_add_options --with-libclang-path=/usr/lib
	ac_add_options --with-unsigned-addon-scopes=app,system
	ac_add_options --with-wasi-sysroot=/usr/share/wasi-sysroot
	ac_add_options --with-branding=browser/branding/aurora
	ac_add_options --host=$CHOST
	ac_add_options --target=$CTARGET

	mk_add_options RUSTFLAGS="$RUSTFLAGS"
	# objdir
	mk_add_options MOZ_OBJDIR="$builddir/obj"
	EOF
}

build() {
	# for lto
	ulimit -n 4096

	# can't be set here and fail
	unset RUSTFLAGS

	local thinlto_jobs=${JOBS:-1}

	case "$CARCH" in
	# on this platform, lld seems to not utilise >1 threads for thinlto for some reason.
	# at the same time, having more than 8 also crashes lld for firefox buildsystems (why?).
	aarch64)
		if [ $thinlto_jobs -gt 8 ]; then
			thinlto_jobs=8
		fi
	esac

	export LDFLAGS="$LDFLAGS -Wl,--thinlto-jobs=$thinlto_jobs"

	export CC="clang"
	export CXX="clang++"

	export SHELL=/bin/sh
	export BUILD_OFFICIAL=1
	export MOZILLA_OFFICIAL=1
	export USE_SHORT_LIBNAME=1
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
	export MOZ_APP_REMOTINGNAME=firefoxdeveloperedition
	export MOZBUILD_STATE_PATH="$srcdir"/mozbuild
	# allow unsigned addons for dev-edition
	export MOZ_REQUIRE_SIGNING=
	# disable desktop notifications
	export MOZ_NOSPAM=1
	# Find our triplet JSON
	export RUST_TARGET="$CTARGET"

	# set rpath so linker finds the libs
	export LDFLAGS="$LDFLAGS -Wl,-rpath,$_mozappdir"

	# let firefox do this itself.
	unset CARGO_PROFILE_RELEASE_OPT_LEVEL
	unset CARGO_PROFILE_RELEASE_CODEGEN_UNITS
	unset CARGO_PROFILE_RELEASE_PANIC
	unset CARGO_PROFILE_RELEASE_LTO

	export MOZ_BUILD_DATE="$(date ${SOURCE_DATE_EPOCH:+ -d@${SOURCE_DATE_EPOCH}} "+%Y%m%d%H%M%S")"

	cat > .mozconfig base-mozconfig

	./mach build
}

package() {
	DESTDIR="$pkgdir" ./mach install

	local _png
	for _png in ./browser/branding/aurora/default*.png; do
		local i=${_png%.png}
		i=${i##*/default}
		install -Dm644 "$_png" \
			"$pkgdir"/usr/share/icons/hicolor/"$i"x"$i"/apps/$pkgname.png
	done

	install -Dm644 browser/branding/aurora/content/about-logo.png \
		"$pkgdir"/usr/share/icons/hicolor/192x192/apps/$pkgname.png
	install -Dm644 browser/branding/aurora/content/about-logo@2x.png \
		"$pkgdir"/usr/share/icons/hicolor/384x384/apps/$pkgname.png
	install -Dm644 browser/branding/aurora/content/about-logo.svg \
		"$pkgdir"/usr/share/icons/hicolor/scalable/apps/$pkgname.svg

	install -Dm644 "$srcdir"/firefox-developer-edition.desktop \
		"$pkgdir"/usr/share/applications/firefox-developer-edition.desktop

	install -Dm644 "$srcdir"/distribution.ini \
		"$pkgdir"/$_mozappdir/distribution/distribution.ini

	# install our vendor prefs
	install -Dm644 "$srcdir"/vendor-prefs.js \
		"$pkgdir"/$_mozappdir/browser/defaults/preferences/vendor.js

	# Replace duplicate binary with wrapper
	# https://bugzilla.mozilla.org/show_bug.cgi?id=658850
	install -Dm755 /dev/stdin "$pkgdir"/usr/bin/firefox-developer-edition <<- EOF
	#!/bin/sh
	exec $_mozappdir/firefox "\$@"
	EOF
	rm "$pkgdir"/$_mozappdir/firefox-bin
	ln -sfv /usr/bin/firefox-developer-edition "$pkgdir"/$_mozappdir/firefox-bin
}

sha512sums="
e78c553ed1df36db7eb16d6f393e0afc88ab67dd74e84c30770f807027890ecb90414720b63dc69ec77696acfba674ed5e2730b3a3e524e6de4a335691b25163  firefox-126.0b9.source.tar.xz
3c28e243c1c74a9bd1ea13729926b1cae3ee35843b14be92ff3287a8cd745e95954e1bc842f2add165923d159bb8d2a7bcd92ba8fb2f4017ba1aef892d77aecf  fix-fortify-system-wrappers.patch
cd68b89e29e5f6379fbd5679db27b9a5ef70ea65e51c0d0a8137e1f1fd210e35a8cfb047798e9549bc7275606d7ec5c8d8af1335d29da4699db7acd8bc7ff556  fix-rust-target.patch
305c874fdea3096e9c4c6aa6520ac64bb1c347c4b59db8360096646593fe684c3b5377874d91cecd33d56d1410b4714fbdea2b514923723ecbeff79d51265d9b  fix-webrtc-glibcisms.patch
ff471e5160e313a9657bcbb767a6a581bab9da053cced02bde38d1ef03349472ad3602dba026c10d676d55c1712af772dd2b7bdbfa72b5db498c9ce8a2133423  install-dir.patch
5fa9382c692e4bd6a2634308f24a6526fd12a60a2563d2090056d43a60505df3ec9881bbf54562e69394467529b3b0dc45955afca46ed329af03cea074fff070  lfs64.patch
64b6458099baa225b346b40e3080cea3a5c73a834e39fa4a438af77701e6ded84e2fb0d6fffff8c2533cc43b898fe9f411be023e875acebec452aa2379861f04  no-ccache-stats.patch
56454b30bed9ce079a06b9d3803030912629d3e0feef49321f1404ab5d004528c446231584a6d89ca69e0bb2ee037664287186975405b048ea3ce06d729a7912  python-deps.patch
1c6918dd6655d3a1251bfd4af2e1c561cbb00d540a883b4c1ebf7f5de530d754d9ac07b4b5f56cdab6c511d25c8910ec94043f5733e97501a67abffe1bafaeb1  rust-lto-thin.patch
2518f2fc75b5db30058e0735f47d60fdf1e7adfaeee4b33fb2afb1bd9a616ce943fd88f4404d0802d4083703f4acf1d5ad42377218d025bc768807fbaf7e1609  sandbox-fork.patch
b7d0a6126bdf6c0569f80aabf5b37ed2c7a35712eb8a0404a2d85381552f5555d4f97d213ea26cec6a45dc2785f22439376ed5f8e78b4fd664ef0223307b333e  sandbox-largefile.patch
f8c3555ef6207933cbffbf4fc101a9b4c0d2990c0063162f0f0bde70ef0b46f86bfac42e7110695183424a87948de593f3927b2d8509ede3e4fc7bd8a1fad1ce  sandbox-sched_setscheduler.patch
afabea91b328c5a68eaa20f9099ac7b2d0e7f2423e816b05ed168bdd326a5684fa02de08bf05c6033e9b888f02775d1b0443a00329b7a632ee399122a391c13a  icu74.patch
0b3f1e4b9fdc868e4738b5c81fd6c6128ce8885b260affcb9a65ff9d164d7232626ce1291aaea70132b3e3124f5e13fef4d39326b8e7173e362a823722a85127  stab.h
2ff0cb8e2eb94cee306b488adf6d7f4debbaff6155be3ed3eeee814cdb356e2e60fe38cc29d6c8d10079937fd2c930cfddf283977cf645395d31eaf76c7f0ac2  firefox-developer-edition.desktop
4fd66382983d406ce0cd3586239fb1110224aeb2f9eee6fc931d980a31d18739d001d1f152208a7f1ccaae80867c40bd3d2ac674484617532a8f38049dc745bd  distribution.ini
0423804a3c7c075de128b624110c58994ec8e10819fc09b74fdb4cd5aa6c0365cdafc77770af42f4ce856ad2e8e325a918344c57fb015a680b7e54555b94c6ed  vendor-prefs.js
"
